import java.util.Arrays;

public class text {
    public static void main(String[] args) {
        //在数组中查找一个数据,找到就会返回这个数在数组中的下标，否则返回-1.
        int[] arr={1,2,3,4,5,6,7};
        //法一：逐个查找
        int ret = chaZhao(arr,5);
        System.out.println(ret);
        //法二：二分查找法
        int index = dinarySearch(arr,6);
        System.out.println(index);

    }
    public static int dinarySearch(int[] arr,int key){
        int left = 0;
        int right = arr.length-1;
        while(left<=right){
            int mid = (right+left)/2;
            if(arr[mid] < key){
                left = mid + 1;
            }else if(arr[mid] > key){
                right = mid - 1;
            }else{
                return mid;
            }
        }
        return  -1;
    }
    public static int chaZhao(int[] arr,int key){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==key){
                return i;
            }
        }
        return -1;
    }
    public static void main6(String[] args) {
        //求一个数组的平均值
        int[] arr={1,2,3,4,5,6};
        double ret = avg(arr);
        System.out.println(ret);
    }
    public static double avg(int[] arr){
        double sum = 0.0;
        for(int x:arr){
            sum += x;
        }
        return sum/ arr.length;
    }
    public static void main5(String[] args) {
        //函数实现ToString
        int[] arr1={1,2,3,4,5,6};
        //int[] arr1 = null;
        String ret =myToString(arr1);
        System.out.println(ret);
    }
    public static String myToString(int[] arr1){
        if(arr1 == null || arr1.length == 0){
            return null;
        }
        String ret = "[";
        for (int i = 0; i < arr1.length; i++) {
            ret += arr1[i];
            if(i != arr1.length-1){
                ret += ",";
            }
        }
        ret += "]";
        return ret;
    }
    public static void main4(String[] args) {
        //返回多个值
        int[] ret=fun1();
        System.out.println(Arrays.toString(ret));
    }
    public static int[] fun1(){
        int a=1;
        int b=2;
        return new int[]{a,b};
    }
    public static void main3(String[] args) {
        //数组的函数调用
        int[] array1={1,2,3,4,5};
        System.out.println(Arrays.toString(array1));
        print(array1);
        System.out.println();
    }
    public static void print(int[] array2){
        array2[0]=8;
        System.out.println(Arrays.toString(array2));
    }
    public static void main2(String[] args) {
        //创建一个 int 类型的数组, 元素个数为 100, 并把每个元素依次设置为 1 - 100
        int[] array = new int[100];
        for (int i = 0; i < 100 ; i++) {
            System.out.print((array[i]=i+1)+" ");
            if(array[i]%10==0){
                System.out.println();
            }
        }

        System.out.println();
    }
    public static void main1(String[] args) {
        //遍历数组
        //法一：
        int[] array = {1,2,3,4,5};
        for (int i = 0; i < array.length; i++) {
            //System.out.println(array[i]);
            System.out.print(array[i]+" ");
        }
        System.out.println();
        //法二：
        int[] array2 = {1,2,3,4,5};
        for(int x:array2){
            System.out.print(x+" ");
        }
        System.out.println();
        //法三：利用java自身
        System.out.println(Arrays.toString(array2));
    }
}
